Skip to content

fix: detect pointer cycles in copyExportedFields to prevent stack overflow#1922

Closed
tamish560 wants to merge 3 commits into
stretchr:masterfrom
tamish560:fix/copy-exported-fields-cycle
Closed

fix: detect pointer cycles in copyExportedFields to prevent stack overflow#1922
tamish560 wants to merge 3 commits into
stretchr:masterfrom
tamish560:fix/copy-exported-fields-cycle

Conversation

@tamish560

Copy link
Copy Markdown

What

copyExportedFields now tracks visited pointer addresses to detect and break pointer cycles, preventing stack overflow on recursive data structures.

Why

EqualExportedValues recurses through a value's exported fields via copyExportedFields. When the value contains a pointer cycle (a struct that directly or indirectly points back to itself), the function follows the cycle indefinitely and exhausts the goroutine stack, killing the entire test binary.

Closes #1915

How

  1. Extracted the recursion into copyExportedFieldsWithVisited which accepts a map[uintptr]bool of visited pointer addresses.
  2. For pointer-typed values, the pointer address is checked against the visited map. If already seen, reflect.Zero of the pointer type is returned, breaking the cycle.
  3. copyExportedFields calls copyExportedFieldsWithVisited with a fresh map.
  4. All existing call sites remain unchanged.

The returned zero value for cyclic pointers means both a and b in a cycle like a.Self = a / b.Self = b get Self = nil in their copies, so the comparison reports them as equal.

Testing

Added TestEqualExportedValuesRecursiveStruct which constructs a self-referential struct and verifies EqualExportedValues completes without stack overflow. All existing tests pass unchanged.

@tamish560

Copy link
Copy Markdown
Author

Closing this as a duplicate of #1916 by @cyphercodes, which was opened 19 days earlier and addresses the same issue (#1915). Should have checked for existing PRs before opening this one.

@tamish560 tamish560 closed this Jul 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EqualExportedValues overflow the stack on recursive data structures

1 participant